/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var rt;

window.onload = function() {
	rt = new imgRound();
	rt.init();
	createLastComment();
}

function createLastComment() {
	createCommentDiv();
	var ajax = new Ajax();
	ajax.requestURL = "commend_last.action";
	ajax.method = "GET";
	ajax.onLoading = function() {

	}
	ajax.onCompletion = function() {
		var jsonObj = JSON.parse(ajax.xmlhttp.responseText);
		for ( var i = 0; i < jsonObj.length; i++) {
			fillComment((i + 1), jsonObj[i].commentContent, jsonObj[i].userName);
		}
	}
	ajax.runAJAX();
}

function createCommentDiv() {
	var p = f$("comment");
	if (f$("comment").childNodes.length > 0)
		return;
	var arr = generateRandom(5, 4);
	for ( var i = 1; i <= 5; i++) {
		var _div = document.createElement("div");
		_div.setAttribute("id", "div" + i);
		_div.style.position = "absolute";
		_div.style.zIndex = "100";
		_div.style.height = "150px";
		_div.style.left = ((i - 1) * 170) + "px";
		_div.style.width = "160px";
		_div.style.backgroundRepeat = "no-repeat";
		_div.style.backgroundImage = "url('img/index/Comment"
				+ (arr[i - 1] + 1) + ".gif')";

		var div = document.createElement("div");
		div.setAttribute("id", "comment" + i);
		div.style.wordWrap = "break-word";
		div.style.position = "absolute";
		div.style.width = "120px";
		div.style.height = "80px";
		div.style.left = '20px';
		div.style.top = '50px';
		_div.appendChild(div);
		p.appendChild(_div);
	}
}

function fillComment(idx, tx, name) {
	var div = f$("comment" + idx);
	if (tx.length > 38) {
		tx = "&nbsp;&nbsp;&nbsp;&nbsp;" + tx.substring(0, 38) + "..."
	}
	div.innerHTML = "<div  style='height:70px;'>" + tx + "</div>";
	div.innerHTML += "<div   style='text-align:right;float:bottom'> " + name
			+ "</div>";
}
var curImg;
function imgRound() {
	var r = 240;
	var x = window.screen.width - 30;
	var y = 268;

	var pi = Math.PI;
	var offset = 0;
	var len = 12;
	var avg = 2 * pi / len;
	var arrimg = $(".indexImg");
	var oWhArr = [];
	var hashWh = new HashMap();
	var totalLen = arrimg.length;
	this.init = function() {
		offset = 0;
		for ( var i = 0; i < totalLen; i++) {
			var t = {
				"w" : arrimg[i].width,
				"h" : arrimg[i].height
			};
			hashWh.put(arrimg[i].id, t);
			oWhArr[i] = t;
		}
		for ( var i = 0; i < totalLen; i++) {
			arrimg[i].style.top = (y - oWhArr[i].h / 2 + 300) + "px";
			arrimg[i].style.left = (x - oWhArr[i].w / 2 + 300) + "px";
		}
		this.move();
	}
	this.moveForward = function() {
		offset = (++offset + totalLen) % totalLen;
		this.move();
	}
	this.moveBack = function() {
		offset = (totalLen + offset - 1) % totalLen
		this.move();
	}
	// this.go = function(idx, myurl, desc) {
	// //offset = (totalLen + idx - len / 2 + 1) % totalLen;
	// //if (curImg && window.event.srcElement.id == curImg.id) {
	// openColorBox(idx, myurl, desc);
	// //return;
	// //}
	// //this.move();
	// }

	this.move = function() {
		for ( var i = 0; i < totalLen; i++) {
			if (i < offset || (offset + len) <= i) {
				arrimg[i].style.display = "none";
			}
		}
		for ( var m = 0; m < len; m++) {
			var k = (offset + m) % totalLen;
			var img = arrimg[k];
			if (!img) {
				continue;
			}
			var angle = avg * (m + 1);
			// 保留左半球
			// if(angle<=pi/2 || angle>=3*pi/2){
			// img.style.display="none";
			// continue;
			// }
			img.style.display = "block";
			var rate = Math.abs(Math.cos(angle) * 0.7) + 0.3;
			if (rate != 1) {
				img.style.border = "1px solid #336600";
				img.setAttribute("title", "");
				rate = rate * 0.7;
			} else {
				rate = rate * 0.8;
				// if (angle > pi / 2 && angle < 3 * pi / 2) {
				// curImg = img;
				// }
				img.style.border = "5px solid #336600";
				img.setAttribute("title", "");
			}
			var wh = hashWh.get(img.id);
			$("#" + img.id).animate({
				top : (y - (Math.sin(angle) * r) - rate * wh.h / 2) + "px",
				left : (x + (Math.cos(angle) * r) - rate * wh.w / 2) + "px",
				width : (wh.w * rate) + "px",
				height : (wh.h * rate) + "px"
			}, 200);
		}
	}
}

function prev() {
	rt.moveBack();
}
function next() {
	rt.moveForward();
}

function go() {
	$("img[class='indexImg']").colorbox({
		title : function() {
			return "<font color='red'>"+$(this).attr('yearmonth').substring(0,4)+"年"+$(this).attr('yearmonth').substring(5,6)+"月</font>    "+$(this).attr('titleStr');
		},
		rel : true,
		href : function() {
			return $(this).attr('goUrl');
		},
		speed:300,
		escKey : true,
		initialWidth : 300,
		initialHeight : 100,
		maxWidth:"100%",
		maxHeight:"100%",
		current:"{current}/{total}"
	});
}

document.onmousewheel = function() {
	if (event.wheelDelta > 0) {
		prev();
	} else {
		next();
	}
}
function HashMap() {

	var size = 0;
	var hashtable = new Array();
	this.get = function(key) {
		return hashtable[key];
	}

	this.put = function(key, obj) {
		if (hashtable[key])
			size++;
		hashtable[key] = obj;
	}
	this.containskey = function(key) {
		return hashtable[key];
	}

	this.size = function() {
		return size;
	}
}